//
//  GMLoginDataController.m
//  GuardMan
//
//  Created by steven on 2016/12/29.
//  Copyright © 2016年 steven. All rights reserved.
//

#import "GMLoginDataController.h"

@implementation GMLoginDataController

@synthesize response;
@synthesize userDictionary;

#pragma mark - login with mobile and password
- (void)loginWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    __weak typeof(self)weakSelf = self;
    [self requestWithType:POST url:ACTION_LOGIN params:params success:^(id object) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.response = object;
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 404:
            case 403:
                errorString = @"用户密码错误";
                break;
            case 500:
                errorString = @"创建token时出错";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        
        callback(message ? message : errorString);
    }];
}

#pragma makr - get user with token
- (void)getUserWithCallback:(GMCompletionCallback)callback{
    __weak typeof(self)weakSelf = self;
    [self requestWithType:GET url:ACTION_USER params:nil success:^(id object) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.userDictionary = object[@"data"];
        callback(nil);
    } failure:^(NSInteger error, NSString *message) {
        NSString *errorString = nil;
        
        switch (error) {
            case 400:
                errorString = @"登录过期，请重新登录";
                break;
            case 401:
                errorString = @"登录过期，请重新登录";
                break;
            case 404:
                errorString = @"用户不存在";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;;
                break;
        }
        
        callback(message ? message : errorString);
    }];
}

@end
